package com.usian.article.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.common.exception.CatchCustomException;
import com.usian.common.exception.code.UserStatusCode;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class ArticleMQ {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @RabbitListener(queues = {"DownOrUpQueue"})
    public void mq(Map map){
        Long article_id = (long)map.get("article_id");
        String ee = String.valueOf(map.get("enable"));

        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(article_id);

        Boolean enable = null;
        if (ee.equals("0")){
            enable = false;
        }else if (ee.equals("1")){
            enable = true;
        }else {
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"上下架状态有误!!!");
        }
        apArticleConfig.setIsDown(enable);

        // update ap_article_config set is_down=? where article_id=?    使用lambda表达式实现sql语句
        LambdaUpdateWrapper<ApArticleConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApArticleConfig::getArticleId,apArticleConfig.getArticleId());
        updateWrapper.set(ApArticleConfig::getIsDown,apArticleConfig.getIsDown());
        apArticleConfigService.update(updateWrapper);

    }

}
